Understanding the rotate() Function in CSS
The rotate() function in CSS is part of the transform property. It allows you to rotate an element around a fixed point (by default, the element’s center) in either 2D or 3D space. The rotation is defined using an angle, such as degrees (deg), radians (rad), or turns (turn).
rotate(angle) — rotates an element clockwise by the given angle in 2D space.
rotateX(angle) — rotates an element around the X-axis (3D rotation).
rotateY(angle) — rotates an element around the Y-axis (3D rotation).
rotateZ(angle) — rotates an element around the Z-axis (similar to rotate() in 2D).
In this example, the box rotates 45 degrees clockwise when hovered. The transition property ensures the rotation happens smoothly.
Here, the element not only rotates but also scales and moves slightly upward to create a dynamic hover animation. This combination leverages GPU acceleration for smoother motion.
Use rotate() for visually appealing animations like spinning icons or tilted cards.
Pair with transition or animation for smooth effects.
Be aware of the transform-origin property, which defines the point around which the element rotates (default is center).
Combine rotate() with other transforms for layered motion effects.